package me.ywy.lib_permission;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import me.ywy.lib_permission.core.IPermission;

public class PermissionActivity extends AppCompatActivity {

    private static IPermission permissionListener;

    private static final String PARAM_PERMISSION = "param_permission";
    private static final String PARAM_REQUEST_CODE = "param_request_code";


    private String[] mPermissions;
    private int mRequestCode;

    public static void requestPermission(Context context , String[] permissions , int requestCode , IPermission listener){
        permissionListener = listener;
        Intent intent = new Intent(context , PermissionActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putStringArray(PARAM_PERMISSION, permissions);
        bundle.putInt(PARAM_REQUEST_CODE, requestCode);
        intent.putExtras(bundle);
        context.startActivity(intent);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(0, 0);
        }


    }

    public static final String TAG = "ywy,aop" ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        Bundle bundle = getIntent().getExtras();
        this.mPermissions = bundle.getStringArray(PARAM_PERMISSION);
        this.mRequestCode = bundle.getInt(PARAM_REQUEST_CODE , -1);
        Log.e(TAG , "切面Activity创建");
        if (mPermissions == null || mRequestCode < 0 || permissionListener == null || mPermissions.length == 0) {
            this.finish();
            return;
        }

        if (PermissionUtils.hasPermission(this, mPermissions)) {
            Log.e(TAG , "权限原本就存在");
            permissionListener.ganted();
            finish();
            return;
        }
        Log.e(TAG , "requestCode:" + mRequestCode);
        ActivityCompat.requestPermissions(this, this.mPermissions, this.mRequestCode);


    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0 , 0);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(PermissionUtils.verifyPermission(this , grantResults)){
            permissionListener.ganted();
            Log.e(TAG , "权限通过");
            finish();
            return;
        }

        //用户点击了不再显示
        if (!PermissionUtils.shouldShowRequestPermissionRationale(this, permissions)) {
//            if (permissions.length != grantResults.length) {
//                return;
//            }
//            List<String> deinedList = new ArrayList<>();
//            for (int i = 0; i < grantResults.length; i++) {
//                if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
//                    deinedList.add(permissions[i]);
//                }
//            }
            Log.e(TAG , "系统禁止申请");
            permissionListener.denied();
            finish();
            return;
        }




        //用户取消

        Log.e(TAG , "用户禁止申请");
        permissionListener.cancled();
        finish();
    }
}
